home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!not-for-mail
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: Forward declarations: why won't they work?
- Date: 23 Mar 1996 14:23:04 GMT
- Organization: Clark Internet Services, Inc., Ellicott City, MD USA
- Message-ID: <4j11g8$140@clarknet.clark.net>
- References: <4ivpp0$iut@hustle.rahul.net>
- NNTP-Posting-Host: explorer.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- Theodore Sternberg (strnbrg@rahul.net) wrote:
- : Forward class declarations only seem to work sometimes. When they don't
- : work, I get a compiler error to the effect that "struct foo is an
- : imcomplete type". Can anyone tell me what's going on, i.e. when forward
- : class declarations are and are not possible?
- :
-
- You'll get an error when the compiler would have to know, not just that
- your struct is a struct, but what's IN the struct, in order to compile
- your code.
-
- For example,
-
- struct Foo;
- struct Bar
- {
- Foo *f;
- };
-
- is fine. The compiler knows that Foo is a struct, and that f is therefore
- a pointer-to-struct. The way a compiler sets aside space for a
- pointer-to-struct is independent of the contents of the struct, so the
- compiler is able to deal with the definition of Bar without having to
- know what a Foo looks like. But,
-
- struct Foo;
- struct Bar
- {
- Foo f;
- };
-
- won't work because now you're telling the compiler that a Foo itself, not
- a pointer to it, is a member of Bar, and you are asking the computer to
- set aside space for a Foo itself. To do this, the compiler _does_ have to
- know what a Foo looks like. Since you haven't told it yet, you will get an
- error.
-